package com.nari.ies.ne.nw.server.service.fileScore.impl;

import com.nari.ies.ne.nw.server.entity.PvFileScoreEntity;
import com.nari.ies.ne.nw.server.entity.WindFileScoreEntity;
import com.nari.ies.ne.nw.server.mapper.FileScoreMapper;
import com.nari.ies.ne.nw.server.service.fileScore.IFileScoreService;
import com.nari.ies.ne.nw.server.service.fileScore.IQXHJFileScoreService;
import com.nari.ies.ne.nw.server.util.DataUtil;
import com.nari.ies.ne.nw.server.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class QXHJFileScoreServiceImpl implements IQXHJFileScoreService {
    @Autowired
    private IFileScoreService fileScoreService;
    @Autowired
    private FileScoreMapper fileScoreMapper;
    @Override
    public WindFileScoreEntity FDQXHJFileScore(WindFileScoreEntity windFileScoreEntity) {
        String plantId = windFileScoreEntity.getPlantId();
        //今天00:00
        Date endDate = windFileScoreEntity.getDateTime();
        Date startDate = DateUtil.getYesterdayTime(endDate);
        String fileType = "ZTXX-QXHJ";
        String plantType = "FD";
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_QXHJ";

        //上送文件数量
        int ZTXX_QXHJNum = fileScoreMapper.selectFileNumData(tableName,plantId, fileType, plantType, startDate, endDate);
        //文件满分
        float ZTXX_QXHJFullScore = fileScoreMapper.selectFileFullScore(fileType, plantType);
        //完整率
        float ZTXX_QXHJRate = 0;
        //文件上送率
        float ZTXX_QXHJSendRate = 0;
        //文件得分
        float ZTXX_QXHJScore = 0;
        //文件合格率
        float ZTXX_QXHJQualifiedRate = 0;

        if (ZTXX_QXHJNum > 0) {
            ZTXX_QXHJSendRate = 100;
            ZTXX_QXHJRate = (float) ZTXX_QXHJNum / 96 * 100;
            //文件数据项总得分
            float fileDataScore = fileScoreService.getFileDataScore(plantId, fileType, plantType, startDate, endDate,tableName);
            //文件得分
            ZTXX_QXHJScore = fileDataScore;
            //文件合格率
            ZTXX_QXHJQualifiedRate = ZTXX_QXHJScore / ZTXX_QXHJFullScore * 100;
        }else {
            ZTXX_QXHJRate=0;
            ZTXX_QXHJScore=0;
            ZTXX_QXHJSendRate=0;
            ZTXX_QXHJQualifiedRate=0;
        }
        windFileScoreEntity.setZTXX_QXHJNum(ZTXX_QXHJNum);
        windFileScoreEntity.setZTXX_QXHJRate(ZTXX_QXHJRate);
        windFileScoreEntity.setZTXX_QXHJScore(ZTXX_QXHJScore);
        windFileScoreEntity.setZTXX_QXHJFullScore(ZTXX_QXHJFullScore);
        windFileScoreEntity.setZTXX_QXHJSendRate(ZTXX_QXHJSendRate);
        windFileScoreEntity.setZTXX_QXHJQualifiedRate(ZTXX_QXHJQualifiedRate);

        return windFileScoreEntity;
    }

    @Override
    public PvFileScoreEntity GFQXHJFileScore(PvFileScoreEntity pvFileScoreEntity) {
        String plantId = pvFileScoreEntity.getPlantId();
        //今天00:00
        Date endDate = pvFileScoreEntity.getDateTime();
        Date startDate = DateUtil.getYesterdayTime(endDate);
        String fileType = "ZTXX-QXHJ-TYGZ";
        String plantType = "GF";
        String tableName="HISDB.NW_NE_FILE_DATACHECK_GF_QXHJ";

        //上送文件数量
        int ZTXX_QXHJ_TYGZNum = fileScoreMapper.selectFileNumData(tableName,plantId, fileType, plantType, startDate, endDate);
        //文件满分
        float ZTXX_QXHJ_TYGZFullScore = fileScoreMapper.selectFileFullScore(fileType, plantType);
        //完整率
        float ZTXX_QXHJ_TYGZRate = 0;
        //文件上送率
        float ZTXX_QXHJ_TYGZSendRate = 0;
        //文件得分
        float ZTXX_QXHJ_TYGZScore = 0;
        //文件合格率
        float ZTXX_QXHJ_TYGZQualifiedRate = 0;

        if (ZTXX_QXHJ_TYGZNum > 0) {
            //文件上送率
            ZTXX_QXHJ_TYGZSendRate = 100;
            ZTXX_QXHJ_TYGZRate = (float) ZTXX_QXHJ_TYGZNum/ 96 * 100;
            //文件数据项总得分
            float fileDataScore = fileScoreService.getFileDataScore(plantId, fileType, plantType, startDate, endDate,tableName);
            //文件得分
            ZTXX_QXHJ_TYGZScore = fileDataScore;
            //文件合格率
            ZTXX_QXHJ_TYGZQualifiedRate = ZTXX_QXHJ_TYGZScore / ZTXX_QXHJ_TYGZFullScore * 100;
        }else {
            ZTXX_QXHJ_TYGZRate=0;
            ZTXX_QXHJ_TYGZScore=0;
            ZTXX_QXHJ_TYGZSendRate=0;
            ZTXX_QXHJ_TYGZQualifiedRate=0;
        }
        pvFileScoreEntity.setZTXX_QXHJ_TYGZNum(ZTXX_QXHJ_TYGZNum);
        pvFileScoreEntity.setZTXX_QXHJ_TYGZRate(ZTXX_QXHJ_TYGZRate);
        pvFileScoreEntity.setZTXX_QXHJ_TYGZScore(ZTXX_QXHJ_TYGZScore);
        pvFileScoreEntity.setZTXX_QXHJ_TYGZFullScore(ZTXX_QXHJ_TYGZFullScore);
        pvFileScoreEntity.setZTXX_QXHJ_TYGZQualifiedRate(ZTXX_QXHJ_TYGZSendRate);
        pvFileScoreEntity.setZTXX_QXHJ_TYGZSendRate(ZTXX_QXHJ_TYGZQualifiedRate);

        return pvFileScoreEntity;
    }


}
